<template>
  <div>
    <Header scroll placeholder></Header>
    <div class="flex justify-center item-center relative">
      <img src="@/assets/activity/ppdt1.jpg" alt="" class="banner-img" />
      <div class="absolute w-full flex justify-center item-center flex-col text-white" style="height: 350px">
        <h2 style="margin: 0">茶叶定制</h2>
        <p style="margin: 10px 0; color: rgba(255, 255, 255, 0.7)">CUSTOMIZE</p>
      </div>
    </div>
    <div class="flex justify-center item-center" style="margin-top: 2rem">
      <div class="btn">茶叶定制</div>
    </div>
    <div class="bg">
      <section class="tea-custom-section" v-for="(item, index) in dataList" :key="item.id">
        <div class="container">
          <div class="tea-custom-content">
            <!-- 左侧传统建筑图片 -->
            <div class="custom-image">
              <img :src="proxy.$buildUrl(item.main_image)" alt="茶叶高端定制" class="custom-building-img" />
            </div>

            <!-- 右侧内容 -->
            <div class="custom-info">
              <div class="custom-header">
                <h2 class="custom-title">{{ item.article_title }}</h2>
                <div class="custom-underline"></div>
              </div>

              <!-- <div class="custom-description">
                <p>
                  {{ item.article_subtitle }}
                </p>
                <p>
                  {{ item.article_abstract }}
                </p>
              </div> -->
              <div v-html="item.article_text" class="custom-description"></div>

              <!-- 制茶工艺流程图片 -->
              <div class="custom-process">
                <div class="process-item" v-for="(image, imgIndex) in item.article_images" :key="imgIndex">
                  <img :src="proxy.$buildUrl(image.url)" alt="茶叶采摘" class="process-img" />
                </div>
                <div class="process-item" v-for="(video, videoIndex) in item.article_videos" :key="videoIndex">
                  <video height="120" controls>
                    <source :src="proxy.$buildUrl(video.url)" type="video/mp4" />
                  </video>
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      <!-- 加载更多 -->
      <div v-if="dataList.length > 0 && hasMore" class="load-more">
        <button class="load-more-btn" @click="loadMore" :loading="loading">
          {{ loading ? '加载中...' : '加载更多' }}
        </button>
      </div>
    </div>
    <Footer></Footer>
    <SettleIn />
  </div>
</template>

<script setup>
  import { getCurrentInstance, ref, computed, onMounted } from 'vue'
  import Header from '@/components/Header.vue'
  import Footer from '@/components/Footer.vue'
  import SettleIn from '@/components/SettleIn.vue'
  import apiGood from '@/api/modules/good'

  const { proxy } = getCurrentInstance()

  // 数据分页查询
  const dataList = ref([])
  const recordCount = ref(0)
  const page = ref(1)
  const limit = ref(6)

  // 是否正在加载
  const loading = ref(false)
  // 是否还有更多数据
  const hasMore = ref(true)
  // 计算属性 - 是否禁用无限滚动
  const disabled = computed(() => loading.value || !hasMore.value)

  // 分页加载数据
  const fetchData = async (currentPage) => {
    if (loading.value) {
      return
    }
    loading.value = true
    apiGood
      .GetArticleList({ page: currentPage, limit: limit.value })
      .then(async (res) => {
        loading.value = false
        recordCount.value = res.count
        page.value = currentPage
        hasMore.value = currentPage * limit.value < recordCount.value
        res.data.forEach((item) => {
          item.article_images = proxy.$parseJsonObject(item.article_images)
          item.article_videos = proxy.$parseJsonObject(item.article_videos)
          if (item.article_images && item.article_images.length > 0) {
            item.main_image = item.article_images[0].url
            item.article_images.splice(0, 1)
          }
        })
        if (page.value === 1) {
          dataList.value = res.data
        } else {
          dataList.value = [...dataList.value, ...res.data]
        }
      })
      .catch((err) => {
        loading.value = false
      })
  }

  // 加载更多
  const loadMore = () => {
    if (!disabled.value) {
      fetchData(page.value + 1)
    }
  }

  // 初始化
  onMounted(() => {
    fetchData(1)
  })
</script>

<style scoped>
  .btn {
    background-color: #1f7260;
    border: 1px solid white;
    padding: 10px 30px;
    color: white;
  }

  .banner-img {
    height: 350px;
    object-fit: cover;
    width: 100%;
  }

  .bg {
    background: url('@/assets/contact/lxwm_bj.png');
    background-size: 100% 100%;
    padding: 2rem;
    background-position-y: center;
    background-repeat: no-repeat;
    backdrop-filter: opacity(0.1);
  }

  .shadow {
    box-shadow: 0 20px 30px rgba(30, 114, 96, 0.5);
  }

  /* 茶叶定制区域 */
  .tea-custom-section {
    padding: 80px 0;
  }

  .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
  }

  .tea-custom-content {
    display: flex;
    align-items: flex-start;
    gap: 60px;
  }

  .custom-image {
    flex: 1;
    max-width: 500px;
  }

  .custom-building-img {
    width: 100%;
    height: auto;
    border-radius: 12px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
  }

  .custom-info {
    flex: 1.2;
    padding-left: 40px;
  }

  .custom-header {
    margin-bottom: 30px;
  }

  .custom-title {
    font-size: 36px;
    font-weight: bold;
    color: #333;
    margin: 0 0 15px 0;
  }

  .custom-underline {
    width: 60px;
    height: 3px;
    background: linear-gradient(90deg, #1f7260 0%, #4caf50 100%);
    border-radius: 2px;
  }

  .custom-description {
    margin-bottom: 40px;
  }

  .custom-description p {
    font-size: 16px;
    line-height: 1.8;
    color: #666;
    text-align: justify;
    margin: 0;
  }

  .custom-process {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
  }

  .process-item {
    text-align: center;
  }

  .process-img {
    width: 100%;
    height: 120px;
    object-fit: cover;
    border-radius: 8px;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  }

  .process-img:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  }

  /* 加载更多 */
  .load-more {
    text-align: center;
    margin-top: 30px;
  }

  .load-more-btn {
    background: #f8f9fa;
    border: 1px solid #ddd;
    color: #666;
    padding: 12px 30px;
    border-radius: 25px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s ease;
  }

  .load-more-btn:hover {
    background: #52a58a;
    color: white;
    border-color: #52a58a;
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .banner-img {
      height: 200px;
    }

    .tea-custom-content {
      flex-direction: column;
      gap: 40px;
    }

    .custom-info {
      padding-left: 0;
    }

    .custom-process {
      grid-template-columns: repeat(2, 1fr);
      gap: 15px;
    }

    .custom-title {
      font-size: 28px;
    }
  }

  /* 额外的响应式优化 */
  @media (max-width: 1024px) {
    .bg {
      padding: 1.5rem;
    }
  }

  @media (max-width: 768px) {
    .banner-img {
      height: 200px;
    }

    .bg {
      padding: 1rem;
      background-size: cover;
    }
  }

  @media (max-width: 480px) {
    .bg {
      padding: 0.5rem;
    }

    .banner-img {
      height: 200px;
    }

    .btn {
      padding: 8px 20px;
      font-size: 14px;
    }
  }
</style>
